package com.bwsoft.action;

import org.apache.log4j.Logger;

import com.bwsoft.domain.User;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = -6669278205043400482L;

	private static Logger LOG = Logger.getLogger(UserAction.class);

	// 1.0 初始化用户管理界面
	public String index() {
		LOG.info("用户：" + ((User) session.get("User")).getName() + "访问【" + fun.getFunName() + "】");
		// 获取所有人员列表
		userList = userService.getUserList();
		// 获取所有角色列表
		userGroupList = userService.getUserGroupListAbsentAdmin();
		return "index";
	}

	// 2.0 添加新用户
	@SuppressWarnings("unchecked")
	public String saveUser() {
		LOG.info("用户：" + ((User) session.get("User")).getName() + "执行【添加用户】");
		// 保存user
		msg = userService.saveUser(user);

		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());

		return "mapJsonResult";
	}

	// 删除用户,可以批量删除
	@SuppressWarnings("unchecked")
	public String deleteUser() {
		LOG.info("用户：" + ((User) session.get("User")).getName() + "执行【删除用户】");
		msg = userService.deleteUser(msg.msgStr);

		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());

		return "mapJsonResult";
	}

	// 初始化修改页面
	@SuppressWarnings("unchecked")
	public String initModify() {

		// 获取对应的ACLUser对象
		user = userService.getUserInfo(user);

		responseJson.put("user", user);
		return "mapJsonResult";
	}

	/**
	 * 执行更新User操作
	 */
	@SuppressWarnings("unchecked")
	public String saveModify() throws Exception {
		LOG.info("用户：" + ((User) session.get("User")).getName() + "执行【修改用户】");
		msg = userService.updateUser(user);

		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());

		return "mapJsonResult";
	}

	/**
	 * 初始化Ztree
	 */
	public String showTree() {

		try {
			responseListJson = userService.getZtreeList(user);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return "listJsonResult";
	}

	/**
	 * 保存用户的功能
	 */
	@SuppressWarnings("unchecked")
	public String saveTree() {
		LOG.info("用户：" + ((User) session.get("User")).getName() + "执行【修改用户权限树】");
		msg = userService.saveTree(user);

		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());

		return "mapJsonResult";
	}

}
